package com.pn.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.util.*;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

/**
    * 系统用户
    */
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "sys_user")
public class SysLoginUser implements Serializable, UserDetails {

    @TableId(value = "user_id", type = IdType.AUTO)
    private Long userId;

    @TableField(value = "username")
    private String username;

    @TableField(value = "password")
    private String password;

    @TableField(value = "status")
    private Integer status;

    //追加的List<String>属性,用于存放查询到的用户的字符串的权限
    @TableField(exist = false)
    private List<String> allAuthorities;

    //-------------------UserDetails的方法------------------------

    /*
      返回用户名的方法,而我们返回userId,因为后台系统的很多业务中都用到了userId,
      所以在此返回userId,之后可以通过Security拿到当前登录的用户id直接操作;
     */
    @Override
    public String getUsername() {
        return userId.toString();
    }

    //返回密码的方法
    @Override
    public String getPassword() {
        return password;
    }

    //返回用户权限的方法
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if(CollectionUtils.isEmpty(allAuthorities)){
            return Collections.EMPTY_LIST;
        }

        List<GrantedAuthority> grantedAuthorityList = new ArrayList<>();
        for (String authority : allAuthorities) {
            if(StringUtils.hasText(authority)){//当前字符串权限不为空
                if(authority.contains(",")){//包含,分隔的多个权限
                    String[] authArray = authority.split(",");
                    for (String auth : authArray) {
                        //将单个字符串权限转成GrantedAuthority对象权限
                        SimpleGrantedAuthority grantedAuthority = new SimpleGrantedAuthority(auth);
                        grantedAuthorityList.add(grantedAuthority);
                    }
                }else{//单个权限
                    //将单个字符串权限转成GrantedAuthority对象权限
                    SimpleGrantedAuthority grantedAuthority = new SimpleGrantedAuthority(authority);
                    grantedAuthorityList.add(grantedAuthority);
                }
            }
        }
        return grantedAuthorityList;
    }

    //判断账号是否未过期
    @Override
    public boolean isAccountNonExpired() {
        return status == 1;
    }

    //判断账号是否未冻结
    @Override
    public boolean isAccountNonLocked() {
        return status == 1;
    }

    //判断密码是否未过期
    @Override
    public boolean isCredentialsNonExpired() {
        return status == 1;
    }

    //判断账号是否可用
    @Override
    public boolean isEnabled() {
        return status == 1;
    }
}